/**
 * Copyright (c) 2017 hadlinks, All Rights Reserved.
 */
package com.bugull.farm.core.control.task.callable;

import com.bugull.farm.core.store.CommandTask;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * Project Name: bugu-farm-household
 * Package Name: com.bugull.farm.device.shortconn.server.control.task 
 * ClassName: PositionSetCallable 
 * Function: TODO ADD FUNCTION.  
 * date: 2017/10/28 17:11
 * @author songwei (songw@hadlinks.com)
 * @since JDK 1.8 
 */
public class PositionSetCallable extends UpperControlCallable {

    private Logger logger = LogManager.getLogger(UpperControlCallable.class);

    public PositionSetCallable(CommandTask commandTask) {
        super(commandTask);
    }

    @Override
    public Boolean call() throws Exception {

//        //查找设备连接信息
//        String terminalSN = commandTask.getTerminalSN();
//        DeviceCache cache = DeviceCache.getInstance();
//        String channelId = cache.getChannelId(terminalSN);
//        channelId = channelId == null ? "" : channelId;
//        ChannelHandlerContext ctx = cache.getContext(channelId);
//        if (ctx != null) { //检查连接状态，离线不执行任务
//            //拼装下发的命令
//            //命令产生的时间
//            int date = (int) (commandTask.getDate().getTime() / 1000L);
//            try {
//                if (commandTask.getPositionComplete() == null || !commandTask.getPositionComplete()) {
//                    //位置信息上报间隔设置
//                    ByteBuf locationSet = Unpooled.buffer();
//                    locationSet.writeByte(props.getByte("pv.short"));//pv
//                    locationSet.writeByte(props.getByte("byte.blank"));//flag
//                    locationSet.writeShort(props.getInt("byte.blank"));//len
//                    locationSet.writeByte(CommandEnum.CMD_DEV_SHORTCONN_LOCATION_SET.getCmd());//cmd
//                    locationSet.writeInt(date);//date
//                    locationSet.writeByte(commandTask.getPositionSetting().getLocationIntervals());//intervals
//                    locationSet.setShort(2, locationSet.readableBytes() - 4);
//                    ctx.writeAndFlush(locationSet).addListener(new ChannelFutureListener() {
//                        @Override
//                        public void operationComplete(ChannelFuture future) throws Exception {
//                            if (future.isSuccess()) {
//                                //下发成功，缓存命令信息
//                                cache.addLocation(terminalSN, commandTask.getPositionSetting().getLocationIntervals()+"");
//                                logger.debug("设备{}成功下发位置设置指令**Intervals:{}!!!",
//                                        terminalSN, commandTask.getPositionSetting().getLocationIntervals());
//                                //构建命令响应事件监听器
//                                CmdResEventListener listener = new CmdResEventListener();
//                                //构建事件源
//                                AbstractBuguEventManager<CmdResEventListener> AbstractBuguEventManager = new AbstractBuguEventManager(ctx);
//                                //发送成功后注册监听器监听响应
//                                AbstractBuguEventManager.addListener(listener);
//                            }
//                        }
//                    });
//                }
//            } catch (Exception e) {
//                logger.error("设备{}下发控制指令失败!!!", terminalSN, e);
//                return Boolean.FALSE;
//            }
//        } else {
//            return Boolean.FALSE;
//        }
        return Boolean.TRUE;
    }

    public CommandTask getCommandTask() {
        return commandTask;
    }

    public Long getCreateTime() {
        return createTime;
    }
}

